package com.codingke.codingbookstore.ui;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.PopupWindow;

import com.codingke.codingbookstore.R;
import com.codingke.codingbookstore.vo.Address;
import com.codingke.codingbookstore.vo.User;

import java.util.List;

import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.BmobUser;
import cn.bmob.v3.listener.FindListener;
import cn.bmob.v3.listener.UpdateListener;

public class EditAddressActivity extends BaseActivity implements View.OnClickListener{

    private EditText editText_username,editText_phoneNumber,editText_address;
    private Address address;
    private Button button_delete_address,button_set_default_address,button_updateAddress;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit_address);
        editText_username = (EditText) findViewById(R.id.editText_username);
        editText_phoneNumber = (EditText) findViewById(R.id.editText_phoneNumber);
        editText_address = (EditText) findViewById(R.id.editText_address);
        button_delete_address = (Button) findViewById(R.id.button_delete_address);
        button_set_default_address = (Button) findViewById(R.id.button_set_default_address);
        button_updateAddress = (Button) findViewById(R.id.button_updateAddress);
        button_delete_address.setOnClickListener(this);
        button_set_default_address.setOnClickListener(this);
        button_updateAddress.setOnClickListener(this);
        setAddress();
    }

    private void setAddress() {
        address = (Address) getIntent().getSerializableExtra("address");
        editText_username.setText(address.getUsername());
        editText_phoneNumber.setText(address.getPhoneNumber());
        editText_address.setText(address.getAddress());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.button_delete_address:
                deleteAddress();
                break;
            case R.id.button_set_default_address:
                setDefaultAddress();
                break;
            case R.id.button_updateAddress:
                updateAddress();
                break;
        }
    }

    /**
     * 更新地址
     */
    private void updateAddress() {
        String username = editText_username.getText().toString();
        if (TextUtils.isEmpty(username)){
            showToast("请输入收货人姓名");
            return;
        }
        String phoneNumber = editText_phoneNumber.getText().toString();
        if (TextUtils.isEmpty(phoneNumber)){
            showToast("请输入收货人手机号码");
            return;
        }
        String addressInfo = editText_address.getText().toString();
        if (TextUtils.isEmpty(addressInfo)){
            showToast("请输入收货人详细地址");
            return;
        }

        address.setUsername(username);
        address.setPhoneNumber(phoneNumber);
        address.setAddress(addressInfo);

        address.update(this);
        finish();
    }

    /**
     * 设置默认地址
     */
    private void setDefaultAddress() {
        if(address.getIsDefault()){
            showToast("已经是默认地址了");
            return;
        }
        BmobQuery<Address> query = new BmobQuery<>();
        query.addWhereEqualTo("userId", BmobUser.getCurrentUser(this,User.class).getObjectId());
        query.addWhereEqualTo("isDefault",true);

        query.findObjects(this, new FindListener<Address>() {
            @Override
            public void onSuccess(List<Address> list) {

                for (int i=0;i<list.size();i++){
                    Address temp = list.get(i);
                    temp.setIsDefault(false);
                    temp.update(EditAddressActivity.this);
                }
                address.setIsDefault(true);
                address.update(EditAddressActivity.this, new UpdateListener() {
                    @Override
                    public void onSuccess() {
                        showToast("已成功设置为默认地址");
                        finish();
                    }
                    @Override
                    public void onFailure(int i, String s) {
                        showToast("设置默认地址失败");
                    }
                });
            }
            @Override
            public void onError(int i, String s) {
                showToast("设置默认地址失败");
            }
        });

    }

    /**
     * 删除地址
     */
    private void deleteAddress() {
        View view = LayoutInflater.from(this).inflate(R.layout.delete_popupwindow,null);
        //创建PopupWindow(窗体的视图,宽，度)
        final PopupWindow popupWindow = new PopupWindow(view,
                ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setAnimationStyle(android.R.style.Animation_Dialog);
        popupWindow.setOutsideTouchable(true);
        popupWindow.setFocusable(true);
        popupWindow.setTouchable(true);
        popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        view.findViewById(R.id.button_delete).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                address.delete(EditAddressActivity.this);
                popupWindow.dismiss();
                finish();
            }
        });
        view.findViewById(R.id.button3_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        popupWindow.showAtLocation(view, Gravity.BOTTOM,0,0);
    }
}
